package com.jxdinfo.hussar.example.mes.pro.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.example.common.constant.UserConstants;
import com.jxdinfo.hussar.example.common.system.service.ISysUserService;
import com.jxdinfo.hussar.example.common.utils.StringUtils;
import com.jxdinfo.hussar.example.mes.md.domain.MdWorkstation;
import com.jxdinfo.hussar.example.mes.md.service.IMdWorkshopService;
import com.jxdinfo.hussar.example.mes.md.service.IMdWorkstationService;
import com.jxdinfo.hussar.example.mes.pro.domain.*;
import com.jxdinfo.hussar.example.mes.pro.domain.Erp.*;
import com.jxdinfo.hussar.example.mes.pro.service.*;
import com.jxdinfo.hussar.example.mes.qc.domain.QcResult;
import com.jxdinfo.hussar.example.mes.qc.service.IQcResultService;
import com.jxdinfo.hussar.example.mes.wm.domain.WmItemConsume;
import com.jxdinfo.hussar.example.mes.wm.domain.WmProductProduce;
import com.jxdinfo.hussar.example.mes.wm.domain.tx.ItemConsumeTxBean;
import com.jxdinfo.hussar.example.mes.wm.domain.tx.ProductProductTxBean;
import com.jxdinfo.hussar.example.mes.wm.service.IStorageCoreService;
import com.jxdinfo.hussar.example.mes.wm.service.IWmItemConsumeService;
import com.jxdinfo.hussar.example.mes.wm.service.IWmProductProduceService;
import com.jxdinfo.hussar.example.common.system.strategy.AutoCodeUtil;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jxdinfo.hussar.example.common.annotation.Log;
import com.jxdinfo.hussar.example.common.core.controller.BaseController;
import com.jxdinfo.hussar.example.common.core.domain.AjaxResult;
import com.jxdinfo.hussar.example.common.enums.BusinessType;
import com.jxdinfo.hussar.example.common.utils.poi.ExcelUtil;
import com.jxdinfo.hussar.example.common.core.page.TableDataInfo;

/**
 * 生产报工记录Controller
 *
 * @author yinjinlu
 * @date 2022-07-10
 */
@RestController
@RequestMapping("/mes/pro/feedback")
public class ProFeedbackController extends BaseController
{
    @Autowired
    private IProFeedbackService proFeedbackService;

    @Autowired
    private IProTaskService proTaskService;
    //工艺组成
    @Autowired
    private IProRouteProcessService proRouteProcessService;

    @Autowired
    private IProWorkorderService proWorkorderService;

    @Autowired
    private IMdWorkstationService mdWorkstationService;

    @Autowired
    private IWmItemConsumeService wmItemConsumeService;

    @Autowired
    private IWmProductProduceService wmProductProduceService;

    @Autowired
    private IStorageCoreService storageCoreService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Autowired
    private IQcResultService qcResultService;

    @Autowired
    private IErpService erpService;

    @Autowired
    private IMdWorkshopService mdWorkshopService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询生产报工记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProFeedback proFeedback)
    {
        startPage();
        List<String> roleNames = userService.selectrolesUserList(BaseSecurityUtil.getUser().getUserId());
        for (String roleName : roleNames) {
            if (!"超级管理员".equals(roleName) && !"企业管理员".equals(roleName)){
                Long aLong = mdWorkshopService.selectMdWorkshopByUserName(BaseSecurityUtil.getUser().getUserName());
                proFeedback.setWorkshopId(aLong);
            }
        }
//        if (!BaseSecurityUtil.getUser().getUserName().equals("超级管理员")){
//            Long aLong = mdWorkshopService.selectMdWorkshopByUserName(BaseSecurityUtil.getUser().getUserName());
//            proFeedback.setWorkshopId(aLong);
//        }
        List<ProFeedback> list = proFeedbackService.selectProFeedbackList(proFeedback);
        return getDataTable(list);
    }

    /**
     * 导出生产报工记录列表
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:export')")
    @Log(title = "生产报工记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProFeedback proFeedback)
    {
        List<ProFeedback> list = proFeedbackService.selectProFeedbackList(proFeedback);
        ExcelUtil<ProFeedback> util = new ExcelUtil<ProFeedback>(ProFeedback.class);
        util.exportExcel(response, list, "生产报工记录数据");
    }

    /**
     * 获取生产报工记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:query')")
    @GetMapping(value = "/get/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(proFeedbackService.selectProFeedbackByRecordId(recordId));
    }

    /**
     * 新增生产报工记录
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:add')")
    @Log(title = "生产报工记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ProFeedback proFeedback)
    {
//        proFeedback.setQuantityUncheck(proFeedback.getQuantityFeedback());
        //报工的生产任务对应的工段
        MdWorkstation workstation = mdWorkstationService.selectMdWorkstationByWorkstationId(proFeedback.getWorkstationId());
        if(StringUtils.isNotNull(workstation)){
            proFeedback.setProcessId(workstation.getProcessId());
            proFeedback.setProcessCode(workstation.getProcessCode());
            proFeedback.setProcessName(workstation.getProcessName());
        }else {
            return AjaxResult.error("当前生产任务对应的工段不存在！");
        }
        //根据生产任务id得到生产任务
        ProTask task = proTaskService.selectProTaskByTaskId(proFeedback.getTaskId());
        proFeedback.setProcessId(task.getProcessId());
        proFeedback.setProcessCode(task.getProcessCode());
        proFeedback.setProcessName(task.getProcessName());
        //根据生产任务得到该生产任务的工艺组成（主要得到他是否减订单keyFlag）
        ProRouteProcess routeProcess = new ProRouteProcess();
        routeProcess.setRouteId(task.getRouteId());
        routeProcess.setProcessId(task.getProcessId());
        List<ProRouteProcess> proRouteProcesses = proRouteProcessService.selectProRouteProcessList(routeProcess);
        //根据生产订单号和生产任务号检查对应的工艺路线和工序配置是否存在
        if(proRouteProcesses.size() == 0){
            return AjaxResult.error("未找到生产任务对应的工艺工序配置！");
        }else {
            routeProcess = proRouteProcesses.get(0);
            //记录ID
            proFeedback.setRecordId(routeProcess.getRecordId());
            //工艺路线ID
            proFeedback.setRouteId(routeProcess.getRouteId());
            //工序id
            proFeedback.setProcessId(routeProcess.getProcessId());
            //是否减订单
            proFeedback.setKeyFlag(routeProcess.getKeyFlag());
        }
//        //生成报工编号
//        String feedbackCode = autoCodeUtil.genSerialCode(UserConstants.FEEDBACK_CODE,"");
//        proFeedback.setFeedbackCode(feedbackCode);
        proFeedback.setCreateBy(String.valueOf(BaseSecurityUtil.getUser().getUserName()));

        //插入完工报告
        int i = proFeedbackService.insertProFeedback(proFeedback);

        //如果是否需要检化验 isCheck=1 则需要检化验,qc_result生成一条检化验记录
        if(UserConstants.YES.equals(proFeedback.getIsCheck())){
            try
            {
                QcResult qcResult = new QcResult();
                BeanUtils.copyProperties(qcResult,proFeedback);
//                qcResult.setUserName(proFeedback.getNickName());
                qcResult.setRecordCode(autoCodeUtil.genSerialCode(UserConstants.RECORD_CODE,""));
                qcResult.setIsComplete("0");
                qcResult.setFeedbackId(proFeedback.getRecordId());
                qcResult.setFeedbackCode(proFeedback.getFeedbackCode());
                qcResultService.insertQcResult(qcResult);
            }
            catch (Exception e)
            {
                return AjaxResult.error("生成检化验记录失败！");
            }
        }else {
            if(!StringUtils.isNotNull(proFeedback.getQuantityQualified()) || !StringUtils.isNotNull(proFeedback.getQuantityUnquanlified())){
                return AjaxResult.error("请输入合格品和不良品数量！");
            }
        }
        return toAjax(i);
    }

    /**
     * 修改生产报工记录
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:edit')")
    @Log(title = "生产报工记录", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResult edit(@RequestBody ProFeedback proFeedback)
    {
        MdWorkstation workstation = mdWorkstationService.selectMdWorkstationByWorkstationId(proFeedback.getWorkstationId());
        if (!StringUtils.isNotNull(workstation)) {
            return AjaxResult.error("当前生产任务对应的工段不存在！");
        }

        //检查对应的工艺路线和工序配置
        if(StringUtils.isNotNull(proFeedback.getRouteId())&& StringUtils.isNotNull(proFeedback.getProcessId())){
            ProRouteProcess param = new ProRouteProcess();
            param.setRouteId(proFeedback.getRouteId());
            param.setProcessId(proFeedback.getProcessId());
            List<ProRouteProcess> processes = proRouteProcessService.selectProRouteProcessList(param);
            if(CollectionUtil.isEmpty(processes)){
                return AjaxResult.error("未找到生产任务对应的工艺工序配置！");
            }
        }else {
            return AjaxResult.error("当前生产任务对应的工艺工序配置无效！");
        }

        //检查数量
        if(UserConstants.YES.equals(proFeedback.getIsCheck())){
            if(!StringUtils.isNotNull(proFeedback.getQuantityUncheck())){
                return AjaxResult.error("当前工段报工需要经过质检确认，请输入待检测数量!");
            }
        }else {
            if(!StringUtils.isNotNull(proFeedback.getQuantityQualified()) || !StringUtils.isNotNull(proFeedback.getQuantityUnquanlified())){
                return AjaxResult.error("请输入合格品和不良品数量！");
            }
        }
        try
        {
            QcResult qcResult = new QcResult();
            qcResult.setFeedbackId(proFeedback.getRecordId());
            qcResult.setFeedbackCode(proFeedback.getFeedbackCode());
            List<QcResult> qcResults = qcResultService.selectQcResultList(qcResult);
            if (qcResults.size() >0){
                qcResult.setRecordId(qcResults.get(0).getRecordId());
                qcResult.setQuantityFeedback(proFeedback.getQuantityFeedback());
                qcResult.setQuantityUncheck(proFeedback.getQuantityUncheck());
                qcResult.setQuantityQualified(BigDecimal.valueOf(0));
                qcResult.setIsComplete("0");
                qcResult.setStatus(UserConstants.ORDER_STATUS_PREPARE);
                qcResultService.updateQcResult(qcResult);
            }
        }
        catch (Exception e)
        {
            return AjaxResult.error("修改检化验记录失败！");
        }
        return toAjax(proFeedbackService.updateProFeedback(proFeedback));
    }

    /**
     * 删除生产报工记录
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:remove')")
    @Log(title = "生产报工记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(proFeedbackService.deleteProFeedbackByRecordIds(recordIds));
    }

    /**
     * 执行报工
     * 1.更新生产任务和生产工单的进度
     * 2.物料消耗
     * 3.产品产出
     * 4.同步erp完工报告
     * 等
     * @param recordId
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('mes:pro:feedback:edit')")
    @Log(title = "生产报工执行", businessType = BusinessType.UPDATE)
    @Transactional
    @GetMapping("/{recordId}")
    public AjaxResult execute(@PathVariable("recordId") Long recordId){

        if(!StringUtils.isNotNull(recordId)){
            return AjaxResult.error("请先保存报工单据");
        }

        ProFeedback feedback= proFeedbackService.selectProFeedbackByRecordId(recordId);
        if(feedback.getQuantityFeedback().compareTo(BigDecimal.ZERO) !=1){
            return AjaxResult.error("报工数量必须大于0");
        }
        //生产工单
        ProWorkorder workorder = proWorkorderService.selectProWorkorderByWorkorderId(feedback.getWorkorderId());
        //生产任务
        ProTask task = proTaskService.selectProTaskByTaskId(feedback.getTaskId());

        //判断当前生产任务的状态，如果已经完成则不能再报工
        if(UserConstants.ORDER_STATUS_FINISHED.equals(task.getStatus())){
            return AjaxResult.error("当前生产工单的状态为已完成，不能继续报工，请刷新生产任务列表！");
        }
        QcResult qcResult = new QcResult();
        qcResult.setFeedbackId(feedback.getRecordId());
        List<QcResult> qcResults = qcResultService.selectQcResultList(qcResult);
        if (qcResults.size() != 0 && qcResults.get(0).getIsComplete().equals("0") ){
            return  AjaxResult.error("当前报工单未完成检验，无法执行报工！");
        }

//        //仍旧有待检数量时不能执行
//        if(StringUtils.isNotNull(feedback.getQuantityUncheck()) && feedback.getQuantityUncheck().compareTo(BigDecimal.ZERO) >0){
//            return  AjaxResult.error("当前报工单未完成检验（待检数量大于0），无法执行报工！");
//        }

        //更新生产任务的生产数量
        BigDecimal quantityProduced,quantityQuanlify,quantityUnquanlify;
        quantityQuanlify = task.getQuantityQuanlify()==null? new BigDecimal(0):task.getQuantityQuanlify();
        quantityUnquanlify = task.getQuantityUnquanlify() ==null? new BigDecimal(0):task.getQuantityUnquanlify();
        quantityProduced = task.getQuantityProduced()==null? new BigDecimal(0):task.getQuantityProduced();
        //已生产数量
        task.setQuantityProduced(quantityProduced.add(feedback.getQuantityFeedback()));
        //不良品数量
        task.setQuantityUnquanlify(quantityUnquanlify.add(feedback.getQuantityUnquanlified()));
        //合格品数量
        task.setQuantityQuanlify(quantityQuanlify.add(feedback.getQuantityQualified()));
        proTaskService.updateProTask(task);



        //如果是否减定单为是，则需调用erp完工报告
        if(UserConstants.YES.equals(feedback.getKeyFlag())){
            //回传erp完工报告
            JSONObject jsonObject = executeFeedback(feedback);
            DataM dataM = jsonObject.toJavaObject(DataM.class);
            if (dataM.getData().get(0).getM_isSucess()){
                //更新生产工单的生产数量
                if(proRouteProcessService.checkKeyProcess(feedback)){
                    BigDecimal produced = workorder.getQuantityProduced() == null?new BigDecimal(0):workorder.getQuantityProduced();
                    BigDecimal feedBackQuantity = feedback.getQuantityFeedback() ==null?new BigDecimal(0):feedback.getQuantityFeedback();
                    workorder.setQuantityProduced( produced.add(feedBackQuantity));
                    proWorkorderService.updateProWorkorder(workorder);
                }
                //更新报工单的状态
                feedback.setStatus(UserConstants.ORDER_STATUS_FINISHED);
                return AjaxResult.success(proFeedbackService.updateProFeedback(feedback));
            }else {
                return AjaxResult.error(dataM.getData().get(0).getM_errorMsg());
            }
        }else {
            //更新生产工单的生产数量
            if(proRouteProcessService.checkKeyProcess(feedback)){
                BigDecimal produced = workorder.getQuantityProduced() == null?new BigDecimal(0):workorder.getQuantityProduced();
                BigDecimal feedBackQuantity = feedback.getQuantityFeedback() ==null?new BigDecimal(0):feedback.getQuantityFeedback();
                workorder.setQuantityProduced( produced.add(feedBackQuantity));
                proWorkorderService.updateProWorkorder(workorder);
            }
            //更新报工单的状态
            feedback.setStatus(UserConstants.ORDER_STATUS_FINISHED);
            return AjaxResult.success(proFeedbackService.updateProFeedback(feedback));
        }

    }

    /**
     * 执行产品产出入线边库动作
     * @param record
     */
    private void executeProductProduce(WmProductProduce record ,ProFeedback feedback){
        List<ProductProductTxBean> beans = wmProductProduceService.getTxBeans(record.getRecordId());
        for (ProductProductTxBean bean : beans) {
            bean.setWarehouseId(feedback.getWarehouseId());
        }
        storageCoreService.processProductProduce(beans);
        record.setStatus(UserConstants.ORDER_STATUS_FINISHED);
        wmProductProduceService.updateWmProductProduce(record);
    }

    /**
     * 执行物料消耗库存动作
     * @param record
     */
    private void executeItemConsume(WmItemConsume record){
        //需要在此处进行分批次领料的线边库扣减
        List<ItemConsumeTxBean> beans = wmItemConsumeService.getTxBeans(record.getRecordId());
        storageCoreService.processItemConsume(beans);
        record.setStatus(UserConstants.ORDER_STATUS_FINISHED);
        wmItemConsumeService.updateWmItemConsume(record);
    }

    /**
     * 同步ERP完工报告
     * @param feedback
     * @return
     */
    private JSONObject executeFeedback(ProFeedback feedback){
        CompRptDatas compRptDatas = new CompRptDatas();
        //Other
        compRptDatas.setOtherID(String.valueOf(feedback.getWorkorderId()));  //工单id
        compRptDatas.setDocNo(feedback.getWorkorderCode());  //工单code
        //Org
        Org org = new Org();       // 死数据
        org.setID(1002401113575156L);                                   //类型转换
        org.setCode("001");
        org.setName("山东鲁银新材料科技有限公司");
        compRptDatas.setOrg(org);
        compRptDatas.setDescription("");
        //CompleteDate
        compRptDatas.setCompleteDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(feedback.getFeedbackTime()));  //完工时间     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
        //CompleteDocType
        CompleteDocType completeDocType = new CompleteDocType();   //完工类型
        completeDocType.setID(1002401113821550L);                  //类型转换
        completeDocType.setCode("1");
        completeDocType.setName("产品完工报告");
        compRptDatas.setCompleteDocType(completeDocType);
        //Direction
        compRptDatas.setDirection(0);
        //HandleDept
        HandleDept handleDept = new HandleDept();
        handleDept.setID(0);                        //经手部门id
        handleDept.setCode("");                     //经手部门编码
        handleDept.setName("");                     //经手部门名称
        compRptDatas.setHandleDept(handleDept);
        //HandlePerson
        HandlePerson handlePerson = new HandlePerson();
        handlePerson.setID(0);                              //经手人id
        handlePerson.setCode(feedback.getUserName());       //经手人编码
        handlePerson.setName(feedback.getNickName());       //经手人名称
        compRptDatas.setHandlePerson(handlePerson);
        //MO
        MO mo = new MO();
        mo.setID(feedback.getWorkorderId());           //订单编号            //类型转换
        mo.setCode(feedback.getWorkorderCode());        //订单编码
        mo.setName(feedback.getWorkorderName());        //订单名称
        compRptDatas.setMO(mo);
        //PLS
        PLS pls = new PLS();
        pls.setID(0);                       //生产线日计划id
        pls.setCode("");                    //生产线日计划编码
        pls.setName("");                    //生产线日计划名称
        compRptDatas.setPLS(pls);
        //WhshipmentReason
        compRptDatas.setWhshipmentReason(0);            //退库理由
        //ProductBaseUOM
        ProductBaseUOM productBaseUOM = new ProductBaseUOM();
        productBaseUOM.setID(0);                //生产副单位id
        productBaseUOM.setCode("");             //生产副单位编码
        productBaseUOM.setName("");             //生产副单位名称
        compRptDatas.setProductBaseUOM(productBaseUOM);
        //ProductUOM
        ProductUOM productUOM = new ProductUOM();
        productUOM.setID(0);                    //生产单位id
        productUOM.setCode("");                 //生产单位编码
        productUOM.setName(feedback.getUnitOfMeasure());       //生产单位名称
        compRptDatas.setProductUOM(productUOM);
        //PUToPBURate
        compRptDatas.setPUToPBURate(0);             //生产主-生产副
        //CompleteQty
        compRptDatas.setCompleteQty(feedback.getQuantityFeedback());   //完工数量
        //CompleteQtyCoUOM
        compRptDatas.setCompleteQtyCoUOM(feedback.getQuantityFeedback());  //完工数量
        //CompleteQtyWhUOM
        compRptDatas.setCompleteQtyWhUOM(feedback.getQuantityFeedback());  //完工数量
        //EligibleQtyCoUOM
        compRptDatas.setEligibleQtyCoUOM(feedback.getQuantityQualified());  //合格数量
        //ReworkingQtyCoUOM
        compRptDatas.setReworkingQtyCoUOM(feedback.getQuantityFeedback().subtract(feedback.getQuantityQualified()).subtract(feedback.getQuantityUnquanlified()));  //待返工数量
        //ScrapQtyCoUOM
        compRptDatas.setScrapQtyCoUOM(feedback.getQuantityUnquanlified());  //报废数量
        //EligibleQty
        compRptDatas.setEligibleQty(feedback.getQuantityQualified());  //合格数量
        //ReworkingQty
        compRptDatas.setReworkingQty(feedback.getQuantityFeedback().subtract(feedback.getQuantityQualified()).subtract(feedback.getQuantityUnquanlified()));    //待返工数量
        //ScrapQty
        compRptDatas.setScrapQty(feedback.getQuantityUnquanlified());            //报废数量
        //ReworkReason
        compRptDatas.setReworkReason(0);    //返工原因
        //ScrapReason
        compRptDatas.setScrapReason(0);   // 报废原因
        //BusinessDate
        compRptDatas.setBusinessDate(new Date().toString());
        //ActualRcvTime
        compRptDatas.setActualRcvTime(new Date());   //实际入库时间
        //RcvWh
        RcvWh rcvWh = new RcvWh();
        rcvWh.setID(feedback.getWarehouseId());               //存储地点id     //类型转换
        rcvWh.setCode(feedback.getWarehouseCode());            // 存储地点编码
        rcvWh.setName(feedback.getWarehouseName());             //存储地点
        compRptDatas.setRcvWh(rcvWh);
        //RcvOrg
        RcvOrg rcvOrg = new RcvOrg();
        rcvOrg.setID(1002401113575156L);             //入库组织
        rcvOrg.setCode("001");         //入库组织
        rcvOrg.setName("山东鲁银新材料科技有限公司");         //入库组织
        compRptDatas.setRcvOrg(rcvOrg);
        //CompleteRptRcvLines
        List<CompleteRptRcvLines> completeRptRcvLinesList = new ArrayList<>();
//        CompleteRptRcvLines completeRptRcvLines = new CompleteRptRcvLines();
        //CompleteRpt
        CompleteRpt completeRpt = new CompleteRpt();
        completeRpt.setID(0);
        completeRpt.setCode("");
        completeRpt.setName("");
        //SourceCompleteRptRcv
        SourceCompleteRptRcv sourceCompleteRptRcv = new SourceCompleteRptRcv();
        sourceCompleteRptRcv.setID(feedback.getWorkorderId());          //来源完工报告
        sourceCompleteRptRcv.setCode(feedback.getWorkorderCode());
        sourceCompleteRptRcv.setName(feedback.getWorkorderName());
        //SourceCompleteRptRcvLine
        SourceCompleteRptRcvLine sourceCompleteRptRcvLine = new SourceCompleteRptRcvLine();
        sourceCompleteRptRcvLine.setID(0);          //来源完工报告入库子表
        sourceCompleteRptRcvLine.setCode("");
        sourceCompleteRptRcvLine.setName("");
        //Wh
        Wh wh = new Wh();
        wh.setID(feedback.getWarehouseId());                   //类型转换
        wh.setCode(feedback.getWarehouseCode());
        wh.setName(feedback.getWarehouseName());
        //Bin
        Bin bin = new Bin();
        bin.setID(0);           //  库位
        bin.setCode("");
        bin.setName("");
        //CompleteRptRcvLines
        CompleteRptRcvLines completeRptRcvLines = new CompleteRptRcvLines();
        completeRptRcvLines.setLineNum(10);         //  行号
        completeRptRcvLines.setGrade(2011);           //  等级
        completeRptRcvLines.setStorageType(4);          //  	存储类型
        completeRptRcvLines.setElement(0);              //成分
        completeRptRcvLines.setCostPercent(0);          //成本比例
        completeRptRcvLines.setRcvLotMaster(0);         //入库批号主档
        completeRptRcvLines.setRcvLotNo(autoCodeUtil.genSerialCode(UserConstants.COMPRPTDATA_CODE,""));    //入库批号
        completeRptRcvLines.setRcvQtyByProductUOM(feedback.getQuantityFeedback());   //生产单位
        completeRptRcvLines.setRcvQtyByWhUOM(feedback.getQuantityFeedback());        //库存单位
        completeRptRcvLines.setRcvQtyByCostUOM(feedback.getQuantityFeedback());     // 成本单位
        completeRptRcvLines.setCUD(0);
        completeRptRcvLines.setRemark("");
        completeRptRcvLines.setRcvLotEffectivedate("");     //入库批号生效期
        completeRptRcvLines.setRcvValiddate(0);             //	入库批号有效期天数
        completeRptRcvLines.setCompleteRpt(completeRpt);
        completeRptRcvLines.setSourceCompleteRptRcv(sourceCompleteRptRcv);
        completeRptRcvLines.setSourceCompleteRptRcvLine(sourceCompleteRptRcvLine);
        completeRptRcvLines.setWh(wh);
        completeRptRcvLines.setBin(bin);
        completeRptRcvLinesList.add(completeRptRcvLines);

        compRptDatas.setCompleteRptRcvLines(completeRptRcvLinesList);

        //OwnerOrg
        OwnerOrg ownerOrg = new OwnerOrg();
        ownerOrg.setID(1002401113575156L);              //  货主组织id
        ownerOrg.setCode("001");           //  货主组织编码
        ownerOrg.setName("山东鲁银新材料科技有限公司");           //  货主组织名称
        compRptDatas.setOwnerOrg(ownerOrg);
        //ItemMaster
        ItemMaster itemMaster = new ItemMaster();
        itemMaster.setID(feedback.getItemId());           //料品id             //类型转换
        itemMaster.setCode(feedback.getItemCode());       //料品编码
        itemMaster.setName(feedback.getItemName());       //料品名称
        compRptDatas.setItemMaster(itemMaster);
        //IsSpecial
        compRptDatas.setIsSpecial(true);     //参数专项
        //DocStatus
        compRptDatas.setDocStatus(2);   //单据状态

        //        System.out.println(jsonObject);
        return erpService.proFeedbackToErp(compRptDatas);
    }

}
